import { Injectable } from '@angular/core';
import { map } from 'rxjs/operators';
import { ApiService } from '@core/services/http/api.service';
import { ApesMessageService } from '@shared/apes/message';

@Injectable({
  providedIn: 'root',
})
export class AssemblyDrawingApiService {

  constructor(
    private apiService: ApiService,
    private msgService: ApesMessageService,
  ) {
  }

  // 查询是否服务车型
  queryMotorcycleType(param) {
    let api = 'fn.service.checkMotorcycleType';
    let data = {
      ...param,
    };
    // console.log('查询服务车型开始', data);
    return this.apiService.post(api, data).pipe(map(((resData: any) => {
        // console.log('查询服务车型结束1',resData);
        return resData;
        // if (resData) {
        //   return false;
        // } else {
        // this.msgService.error('无该不服务车型适用商品');
        //   return true;
        // }
      })),
    );
  }

  // 解析车型配置
  queryCarConfig(param) {
    let api = 'fn.api.getCarDetailInfoForNew';
    let data = {
      'PZCXID': param['jekunModelCode'],
    };
    // console.log('解析车型开始', data);
    return this.apiService.post(api, data).pipe(map(((resData: any) => {
        // console.log('解析车型结束1',resData);
        if (resData) {
          return resData;
        } else {
          this.msgService.error('未解析到对应的车型配置');
          return {};
        }
      })),
    );
  }

  // 解析VIN
  queryCarVIN(param) {
    let data = {
      'vin': param['vin'],  // 车架号
      'QCCPMC': param['QCCPMC'],  // 汽车厂牌名称
      'QCCPID': param['QCCPID'],  // 汽车厂牌ID
      'PZCXID': param['PZCXID'],    // 配置车型编码
      'saleDept': param['saleDept'],  // 门店信息
      'publicPage': param['publicPage'], // 公共页查询
    };
    let api = 'auto.parts.queryVin';
    // console.log('解析VIN开始', data);
    return this.apiService.post(api, data).pipe(map(((resData: any) => {
        // console.log('解析VIN结束1',resData);
        if (resData) {
          return resData;
        } else {
          this.msgService.error('未解析到对应的车型VIN');
          return {};
        }
      })),
    );
  }

  // 解析主组数据
  queryMainGroup(param) {
    let data = {
      'vin': param['vin'],  // 车架号
      'id': param['selectorId'],  // 选择ID
      'brandCode': param['brandCode'],    // 品牌代码
    };
    let api = 'auto.parts.queryMainGroup';
    // console.log('解析主组数据开始', data);
    return this.apiService.post(api, data).pipe(map(((resData: any) => {
        // console.log('解析主组数据结束1',resData);
        if (resData && resData.length > 0) {
          return resData;
        } else {
          this.msgService.error('未解析到对应的主组数据');
          return [];
        }
      })),
    );
  }

  // 解析子组数据
  querySubGroup(param) {
    let selectorId;
    if (param['MainGroup'].selectorId && param['MainGroup'].selectorId != '0') {
      selectorId = param['MainGroup'].selectorId;
    } else {
      selectorId = param['selectorId'];
    }
    let data = {
      'vin': param['vin'],                // 车架号
      'id': param['MainGroup'].id,     // 选择子组ID
      'brandCode': param['brandCode'],    // 品牌代码
      'selectorId': selectorId,      // 选择ID
    };
    let api = 'auto.parts.querySubGroup';
    // console.log('解析子组数据开始', data);
    return this.apiService.post(api, data).pipe(map(((resData: any) => {
        // console.log('解析子组数据结束1',resData);
        if (resData && resData.length > 0) {
          return resData;
        } else {
          this.msgService.error('未解析到对应的子组数据');
          return [];
        }
      })),
    );
  }

  // 查询热点图
  queryImgPoint(param) {
    let selectorId;
    if (param['mainSelectorId'] != null) {
      selectorId = param['mainSelectorId'];
    } else {
      selectorId = param['selectorId'];
    }
    let data = {
      'vin': param['vin'],                                // 车架号
      'id': param['MainGroup'].id,                        // 选择子组ID
      'brandCode': param['brandCode'],                    // 品牌代码
      'selectorId': selectorId,                           // 选择ID
      'mainGroupId': param['mainGroupId'],                // 选择主组ID
    };
    let api = 'auto.parts.queryImgPoint';
    // console.log('查询热点图开始', data);
    return this.apiService.post(api, data).pipe(map(((resData: any) => {
        // console.log('查询热点图结束1',resData);
        if (resData) {
          return resData;
        } else {
          this.msgService.error('未查询到热点图数据');
          return [];
        }
      })),
    );
  }

  // 查询配件
  queryParts(param) {
    let selectorId;
    if (param['mainSelectorId'] != null) {
      selectorId = param['mainSelectorId'];
    } else {
      selectorId = param['selectorId'];
    }
    let data = {
      'vin': param['vin'],                                // 车架号
      'id': param['MainGroup'].id,                        // 选择子组ID
      'brandCode': param['brandCode'],                    // 品牌代码
      'selectorId': selectorId,                           // 选择ID
      'PZCXID': param['PZCXID'],                          // 配置车型ID
      'QCCPMC': param['QCCPMC'],                          // 汽车厂牌名称
      'mainGroupId': param['mainGroupId'],                // 选择主组ID
    };
    let api = 'auto.parts.queryParts';
    // console.log('查询配件开始', data);
    return this.apiService.post(api, data).pipe(map(((resData: any) => {
        // console.log('查询配件结束1',resData);
        if (resData) {
          return resData;
        } else {
          this.msgService.error('未查询到配件数据');
          return [];
        }
      })),
    );
  }

  // 查询配件名称
  searchParts(param) {
    let data = {
      'vin': param['vin'],                                // 车架号
      'keywords': param['keywords'],                      // 关键字
      // 'brandCode': param['brandCode'],                    // 品牌代码
      // 'selectorId': param['selectorId'],                  // 选择ID
    };
    // let api = 'auto.parts.searchParts';
    let api = 'auto.parts.searchPartsByVinKeywords';
    // console.log('查询配件名称开始', data);
    return this.apiService.post(api, data).pipe(map(((resData: any) => {
        // console.log('查询配件名称结束1',resData);
        if (resData) {
          return resData;
        } else {
          this.msgService.error('未查询到该配件名数据');
          return {};
        }
      })),
    );
  }

  // 整合查询参数数据
  integrateSearchData(param) {
    let partsType = param.partsType;

    let data: any = {
      'vin': param['vin'],                                // 车架号
      'keywords': param['keywords'],                      // 关键字
      'brandCode': param['brandCode'],                    // 品牌代码
      'PZCXID': param['PZCXID'],
      'QCCPMC': param['QCCPMC'],
      'QCCPID': param['QCCPID'],
    };
    let searchData: any = {
      'keywords': param['keywords'],
      'partsTagName': param['partsTag']['name'],
      'partsTagId': param['partsTag']['id'],
      'partsTag': param['partsTag'],
    };

    let setData = {
      condition: [
        { field: partsType.field, operator: 'LIKE', value: partsType.value },
        { field: 'relationType', operator: 'EQ', value: param['relationType'] },
        { field: 'isQuery', operator: 'EQ', value: param['isQuery'] },
        { field: 'dataCenterCondition', operator: 'EQ', value: data },
        { field: 'searchContent', operator: 'EQ', value: searchData },
      ],
      conditionModel: param['model'],
      size: param['size'],
      page: param['page'],
      queryStock: param['queryStock'],
      filterCondition: param['filterCondition'],
    };
    // console.log('查询参数',setData);
    return setData;
  }

  // 查询匹配配件名称
  searchMatchParts(param) {
    let setData = this.integrateSearchData(param);
    let api;
    // let api = 'apes.auto.parts.QueryOfParts'; // 原版
    // let api = 'apes.auto.parts.fuzzyOfPartsReal'; // 车宝库存

    if (param['queryStock']) {
      api = 'apes.auto.parts.fuzzyOfPartsReal'; // 车宝库存
    } else {
      api = 'apes.auto.parts.QueryOfParts'; // 通用商品
    }


    // console.log('查询匹配配件名称开始', setData);
    return this.apiService.post(api, setData).pipe(map(((resData: any) => {
        // console.log('查询匹配配件名称结束1', resData);
        if (resData) {
          return resData;
        } else {
          this.msgService.error('未查询到该匹配配件名数据');
          return {};
        }
      })),
    );
  }

  // 查询匹配配件过滤条件
  searchFilterCondition(param) {
    let setData = {
      filterCondition: param.partsTag.filterCondition,
      queryStock: param.partsTag.queryStock,
      PZCXID: param.PZCXID,
      id: param.partsTag.id,
    };

    let api = 'fn.practicalFunction.config.getFilter';
    // console.log('查询匹配配件过滤条件开始', setData);
    return this.apiService.post(api, setData).pipe(map(((resData: any) => {
        // console.log('查询匹配配件过滤条件结束1', resData);
        if (resData) {
          return resData;
        } else {
          this.msgService.error('未查询到该匹配配件过滤条件数据');
          return {};
        }
      })),
    );
  }

  // 查询价格前置商品
  searchPriceParts(param) {
    let setData = this.integrateSearchData(param);

    let api = 'fn.priceForwardService.queryPriceForward';
    console.log('查询价格前置商品开始', setData);
    return this.apiService.post(api, setData).pipe(map(((resData: any) => {
        console.log('查询价格前置商品结束1', resData);
        if (resData) {
          return resData;
        } else {
          this.msgService.error('未查询到该价格前置商品数据');
          return {};
        }
      })),
    );
  }

  // 查询价格前置商品
  searchMismatchPriceParts(param) {
    let setData = this.integrateSearchData(param);

    let api = 'fn.priceForwardService.suppInquireProduct';
    console.log('查询价格前置商品开始', setData);
    return this.apiService.post(api, setData).pipe(map(((resData: any) => {
        console.log('查询价格前置商品结束1', resData);
        if (resData) {
          return resData;
        } else {
          this.msgService.error('未查询到该价格前置商品数据');
          return {};
        }
      })),
    );
  }

  // 查询智能配件名称
  searchIntelligentParts(param) {
    let setData = this.integrateSearchData(param);

    let api = 'auto.parts.fuzzyOfParts';
    // console.log('查询智能配件名称开始', setData);
    return this.apiService.post(api, setData).pipe(map(((resData: any) => {
        // console.log('查询智能配件名称结束1', resData);
        if (resData) {
          return resData;
        } else {
          this.msgService.error('未查询到该智能配件名数据');
          return {};
        }
      })),
    );
  }

  // 查询三头六臂配件名称
  searchSanTouInquireParts(param) {
    let data = {
      'vin': param['vin'],                                // 车架号
      'keywords': param['keywords'],                      // 关键字
      'partsTag': param['partsTag'],                     //
      'saleDept': param['saleDept'],                     //
      'lossDept': param['model']['getCarDept'],
      'carLicense': param['carLicense'],
      'relationType': param['relationType'],
      'page': param['page'],
      'size': param['size'],
    };
    let api = 'fn.santouliubiService.santouInquireProduct';
    // console.log('查询三头六臂配件开始', data);
    return this.apiService.post(api, data).pipe(map(((resData: any) => {
        // console.log('查询三头六臂配件结束',resData);
        if (resData) {
          return resData;
        } else {
          this.msgService.error('未查询到该配件名数据');
          return {};
        }
      })),
    );
  }

  searchSanTouInquirePrice(param) {
    let data = {
      'vin': param['vin'],                                // 车架号
      'keywords': param['keywords'],                      // 关键字
      'partsTag': param['partsTag'],                     //
      'saleDept': param['saleDept'],                     //
      'lossDept': param['model']['getCarDept'],
      'carLicense': param['carLicense'],
      'relationType': param['relationType'],
      'page': param['page'],
      'size': param['size'],
    };
    let api = 'fn.santouliubiService.marketPrice';
    // console.log('查询三头六臂配件价格开始', data);
    return this.apiService.post(api, data).pipe(map(((resData: any) => {
        // console.log('查询三头六臂配件价格结束',resData);
        if (resData) {
          return resData;
        } else {
          this.msgService.error('未查询到该配件价格数据');
          return {};
        }
      })),
    );
  }
}
